package net.coding.li709.materialrefresher.keys;

import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataView;
import org.spongepowered.api.data.key.Key;
import org.spongepowered.api.data.manipulator.DataManipulatorBuilder;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.data.value.mutable.MutableBoundedValue;

import java.util.Optional;

public class FlanDataManipulatorBuilder implements DataManipulatorBuilder<FlanData, ImmutableFlanData> {

    @Override
    public FlanData create() {
        return new FlanData(false,"");
    }

    @Override
    public Optional<FlanData> createFrom(DataHolder dataHolder) {
        return Optional.of(dataHolder.get(FlanData.class).orElse(new FlanData(false,"")));
    }

    @Override
    public Optional<FlanData> build(DataView container) throws InvalidDataException {
        // Note that this should check the Queries.CONTENT_VERSION, but for the sake of demonstration
        // it's not necessary
        if (container.contains(FlanKeys.IS_SPAWN)&&container.contains(FlanKeys.SPAWN_NAME)) {
            final boolean isSpawn = container.getBoolean(FlanKeys.IS_SPAWN.getQuery()).get();
            final String spawnName = container.getString(FlanKeys.SPAWN_NAME.getQuery()).get();
            return Optional.of(new FlanData(isSpawn,spawnName));
        }
        return Optional.empty();
    }
}
